package org.ppassantini.cham.core.business.security.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.ppassantini.cham.core.business.security.dao.MenuDao;
import org.ppassantini.cham.core.business.security.dto.MenuDto;
import org.ppassantini.cham.core.business.security.entity.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ppassantini
 * 
 */
@Repository("menuDao")
public class MenuDaoImpl extends HibernateDaoSupport implements MenuDao {

	@Autowired
	private Mapper mapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.dao.GenericDao#save(java.lang.Object)
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void save(Menu menu) {
		getHibernateTemplate().save(menu);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.dao.GenericDao#update(java.lang.Object
	 * )
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void update(Menu menu) {
		getHibernateTemplate().update(menu);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.dao.GenericDao#delete(java.lang.Object
	 * )
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delete(Menu menu) {
		getHibernateTemplate().delete(menu);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.dao.GenericDao#findByKey(java.lang
	 * .Object)
	 */
	@Override
	public Menu findByKey(Integer idmenu) {
		List<Menu> list = getHibernateTemplate().find(
				"from Menu m where m.idmenu=?", idmenu);

		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.business.security.dao.MenuDao#getMenuByUsername
	 * (java.lang.String)
	 */
	@Override
	public List<Menu> getMenuByUsername(String username) {
		String query = "select m "
				+ "from  Menu m,Profile p,Users u, MenuProfile mp,UserProfile us "
				+ "where u.idusers=us.idusers and "
				+ "us.idprofile=mp.idprofile and  " + "mp.idmenu=m.idmenu and "
				+ "u.username=?";

		List<Menu> list = getHibernateTemplate().find(query, username);

		return list;
	}

	@Override
	public int count() {
		String hql = "select count(menu) from Menu menu";
		List result = getHibernateTemplate().find(hql);
		Integer count = ((Long)result.get(0)).intValue();

		return count;
	}

	@Override
	public List<MenuDto> find(int from, int to) {
		List<MenuDto> list = new ArrayList<MenuDto>();
		String hql = "from Menu menu";
		List<Menu> result = getHibernateTemplate().find(hql);

		for (int i = from; i <= to; i++) {
			list.add(mapper.map(result.get(i), MenuDto.class));
		}

		return list;
	}

}
